<html>
<head><meta charset="utf-8"><title>move out of Box&lt;[T]&gt; · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html">move out of Box&lt;[T]&gt;</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221042977"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221042977" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221042977">(Dec 28 2020 at 06:56)</a>:</h4>
<p>Is there a nice way to do this?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">foo</span>: <span class="nb">Box</span><span class="o">&lt;</span><span class="p">[</span><span class="nb">String</span><span class="p">]</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">([</span><span class="nb">String</span>::<span class="n">new</span><span class="p">(),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">new</span><span class="p">()]);</span><span class="w"></span>
<span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="p">[</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}, {}"</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="221043033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221043033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221043033">(Dec 28 2020 at 06:58)</a>:</h4>
<p>My current best alternative is convoluted, to say the least</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">foo</span>: <span class="nb">Box</span><span class="o">&lt;</span><span class="p">[</span><span class="nb">String</span><span class="p">]</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">([</span><span class="nb">String</span>::<span class="n">new</span><span class="p">(),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">new</span><span class="p">()]);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">foo</span><span class="p">.</span><span class="n">into_vec</span><span class="p">();</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">it</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">foo</span><span class="p">.</span><span class="n">drain</span><span class="p">(</span><span class="o">..</span><span class="p">);</span><span class="w"></span>
<span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="nb">Some</span><span class="p">(</span><span class="n">a</span><span class="p">),</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">b</span><span class="p">),</span><span class="w"> </span><span class="nb">None</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">it</span><span class="p">.</span><span class="n">next</span><span class="p">(),</span><span class="w"> </span><span class="n">it</span><span class="p">.</span><span class="n">next</span><span class="p">(),</span><span class="w"> </span><span class="n">it</span><span class="p">.</span><span class="n">next</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}, {}"</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="221046766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221046766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221046766">(Dec 28 2020 at 08:35)</a>:</h4>
<p>You could unsafely fiddle the box into being a vec.</p>



<a name="221046787"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221046787" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221046787">(Dec 28 2020 at 08:35)</a>:</h4>
<p>(And then pop or into_iter or whatever)</p>



<a name="221047266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221047266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221047266">(Dec 28 2020 at 08:47)</a>:</h4>
<p>I don't think the box -&gt; vec needs to be unsafe (I used <code>into_vec</code> to do just that in the second snippet)</p>



<a name="221047323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221047323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221047323">(Dec 28 2020 at 08:48)</a>:</h4>
<p>but the move out is quite painful; I thought that slice patterns would help here but maybe it's just not possible to use them with move</p>



<a name="221047522"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221047522" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221047522">(Dec 28 2020 at 08:53)</a>:</h4>
<p>(The fact that <code>Box&lt;[T]&gt;</code> doesn't get as much love in std as <code>Vec&lt;T&gt;</code> is a shame, since I use it widely in my codebase because it saves memory. But at least in settings like this it's generally no extra runtime cost to use <code>into_vec</code> and then immediately destructure it; at least I would hope that both versions above translate essentially to <code>let a = foo[0]; let b = foo[1];</code> in the assembly)</p>



<a name="221051584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221051584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221051584">(Dec 28 2020 at 10:22)</a>:</h4>
<p>Oh gosh, you're already using vec. Zulip was acting up and showed one post but not the other.</p>



<a name="221052286"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221052286" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221052286">(Dec 28 2020 at 10:36)</a>:</h4>
<p>That seems like something warranting a <code>TryInto</code> implementation to go from a boxed slice to a boxed array</p>



<a name="221053339"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221053339" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221053339">(Dec 28 2020 at 11:00)</a>:</h4>
<p>Unfortunately that doesn't work with the fancier features of slice patterns like <code>[a, b, .., c]</code> or having multiple matches like <code>match *foo { [a, b] =&gt; ..., [a, b, c] =&gt; ... }</code></p>



<a name="221055490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221055490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221055490">(Dec 28 2020 at 11:43)</a>:</h4>
<blockquote>
<p>The fact that Box&lt;[T]&gt; doesn't get as much love in std as Vec&lt;T&gt; is a shame</p>
</blockquote>
<p>A <code>Box&lt;[T]&gt;</code> mostly makes sense if you have a huge amount of them and really need to save that extra usize compared to <code>Vec</code>. And if it's owned by structs then you can't move out of it or manipulate it in many of the ways you can manipulate a Vec anyway.</p>



<a name="221059891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221059891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221059891">(Dec 28 2020 at 13:17)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> note that if instead of a <code>*</code>-move on the matchee, one uses a <code>box</code> wrapping pattern, it works: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=fddda7a81c0e21594405dc91379c676e">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=fddda7a81c0e21594405dc91379c676e</a></p>



<a name="221069816"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221069816" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221069816">(Dec 28 2020 at 15:58)</a>:</h4>
<p>That only works because u64 is Copy though. You still can't move out of the Box that way <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=ad349c656f5a96e8107a6750b0bc1084">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=ad349c656f5a96e8107a6750b0bc1084</a></p>



<a name="221070209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221070209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221070209">(Dec 28 2020 at 16:03)</a>:</h4>
<p>Makes me think of the DerefMove discussion. I'd want DerefMove to handle this case but no idea how to make it work since I'd want to move the unsized [Foo] out of the box</p>



<a name="221073849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221073849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221073849">(Dec 28 2020 at 16:53)</a>:</h4>
<p><span class="user-mention silent" data-user-id="330154">The 8472</span> <a href="#narrow/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E/near/221055490">said</a>:</p>
<blockquote>
<p>And if it's owned by structs then you can't move out of it or manipulate it in many of the ways you can manipulate a Vec anyway.</p>
</blockquote>
<p>Yes, that's my point. There isn't any good reason for this AFAIK; I don't think there is anything preventing <code>Box&lt;[T]&gt;</code> from having <code>drain</code>, <code>into_iter</code>, and other things like that just like <code>Vec&lt;T&gt;</code>.</p>



<a name="221074304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221074304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221074304">(Dec 28 2020 at 16:59)</a>:</h4>
<p>(Actually, on second thought <code>drain</code> doesn't really make sense on <code>Box&lt;[T]&gt;</code> unless you are draining everything in which case <code>into_iter</code> would be better - I could have used that instead in my code snippet above. And there is an issue with having <code>Box&lt;[T]&gt;::into_iter</code>, namely that <code>x.into_iter()</code> currently gives you <code>slice::IntoIter</code> via auto deref. I think we should do a crater run or something on this use though, because I can't think of any case where it's not a mistake to write that - <code>x.iter()</code> is the clearer and correct way to do that.)</p>



<a name="221074626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221074626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221074626">(Dec 28 2020 at 17:04)</a>:</h4>
<p>the exact same discussion happened about [T;N].into_iter() if you're interested: <a href="https://github.com/rust-lang/rust/pull/65819">https://github.com/rust-lang/rust/pull/65819</a></p>



<a name="221080213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221080213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221080213">(Dec 28 2020 at 18:18)</a>:</h4>
<p>Hm, since it was a year old I was expecting it to be stalled but it looks like there is some forward progress. Since the same future-compat issue blocks both  [T;N].into_iter() and Box&lt;[T]&gt;.into_iter(), is there a way to make them both blocked on the same issue?</p>



<a name="221082081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221082081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221082081">(Dec 28 2020 at 18:44)</a>:</h4>
<p>you should ask it there, it indeed feels like it could be solved at the same time</p>



<a name="221093247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221093247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221093247">(Dec 28 2020 at 21:38)</a>:</h4>
<p><span class="user-mention silent" data-user-id="245339">Nadrieril</span> <a href="#narrow/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E/near/221069816">said</a>:</p>
<blockquote>
<p>That only works because u64 is Copy though. You still can't move out of the Box that way <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=ad349c656f5a96e8107a6750b0bc1084">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=ad349c656f5a96e8107a6750b0bc1084</a></p>
</blockquote>
<p>Oh damn, that's so disappointing. I had a hunch that with <code>box</code> patterns it was gonna work ;_;</p>



<a name="221152309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221152309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221152309">(Dec 29 2020 at 17:47)</a>:</h4>
<p>I suspect the original code doesn’t work because <code>*foo</code> has type <code>[String]</code> which is <code>!Sized</code>. Going through <code>Box&lt;[String; 2]&gt;</code> works: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=d72300ce4eecb0f2b75cb7374e485f65">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=d72300ce4eecb0f2b75cb7374e485f65</a></p>



<a name="221152361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221152361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221152361">(Dec 29 2020 at 17:47)</a>:</h4>
<p>We could add <code>impl&lt;T; const N: usize&gt; TryFrom&lt;Box&lt;[T]&gt;&gt; for Box&lt;[T; N]&gt;</code></p>



<a name="221152410"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221152410" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221152410">(Dec 29 2020 at 17:48)</a>:</h4>
<p>since we already have <code>&amp;[T]</code> → <code>&amp;[T; N]</code></p>



<a name="221153679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221153679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221153679">(Dec 29 2020 at 18:04)</a>:</h4>
<p>wait, we do already have that impl</p>



<a name="221153931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221153931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221153931">(Dec 29 2020 at 18:07)</a>:</h4>
<p>It’s more verbose than I’d hoped, but:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">convert</span>::<span class="n">TryFrom</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">foo</span>: <span class="nb">Box</span><span class="o">&lt;</span><span class="p">[</span><span class="nb">String</span><span class="p">]</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">([</span><span class="nb">String</span>::<span class="n">new</span><span class="p">(),</span><span class="w"> </span><span class="nb">String</span>::<span class="n">new</span><span class="p">()]);</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Ok</span><span class="p">(</span><span class="n">boxed</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="o">&lt;</span><span class="p">[</span><span class="n">_</span><span class="p">;</span><span class="w"> </span><span class="mi">2</span><span class="p">]</span><span class="o">&gt;</span>::<span class="n">try_from</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="p">[</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">boxed</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}, {}"</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="221154007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221154007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221154007">(Dec 29 2020 at 18:08)</a>:</h4>
<p>Inference is confused without specifying the conversion target type</p>



<a name="221154930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221154930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221154930">(Dec 29 2020 at 18:20)</a>:</h4>
<p>I'd love to be able to do</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="o">???</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="o">..</span><span class="p">.}</span><span class="w"></span>
<span class="w">  </span><span class="p">[</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">]</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="o">..</span><span class="p">.}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>though, and TryFrom doesn't solve that</p>



<a name="221161691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221161691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221161691">(Dec 29 2020 at 19:52)</a>:</h4>
<p>Note, in my actual use case <code>foo: Box&lt;[NonCopy]&gt;</code> is a parameter; I wouldn't be using <code>Box&lt;[T]&gt;</code> if it had a fixed length</p>



<a name="221161865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221161865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221161865">(Dec 29 2020 at 19:55)</a>:</h4>
<p>(oh, you mean to use <code>[T; 2]</code> because that's the size of the pattern)</p>



<a name="221161959"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221161959" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221161959">(Dec 29 2020 at 19:56)</a>:</h4>
<p>I don't see why it matters that <code>*foo</code> is <code>!Sized</code> though, because it's an lvalue in the match statement</p>



<a name="221161991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221161991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221161991">(Dec 29 2020 at 19:56)</a>:</h4>
<p>we shouldn't be copying it to the stack in the first place</p>



<a name="221194206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/move%20out%20of%20Box%3C%5BT%5D%3E/near/221194206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/move.20out.20of.20Box.3C.5BT.5D.3E.html#221194206">(Dec 30 2020 at 06:55)</a>:</h4>
<p>I don’t know to what extent that lvalue reasoning applies when moving out of <code>Box</code>. Such moves are special cases in the language and the compiler since there’s no <code>DerefMove</code> trait, so I wouldn’t be surprised if the semantics are not completely well-specified and/or the implementation buggy when combining this with DST <code>[T]</code>. I think it’s worth filing a bug, maybe this can can be made to work?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>